package com.ding.easycontroller.utils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 将ant风格路径转换为正则表达式
 */
public class AntUtils {

    //将ant风格路径替换为正则表达式
    public static String antPattern(String ant) {
        return transfer(ant);
    }

    //进行格式转化
    private static String transfer(String ant) {
        ant = ant.replace("/**", "(/[^/]*)*");
        ant = ant.replace("/*", "/[^/]*");
        return ant;
    }

    private static Pattern pattern=Pattern.compile("\\{([^/]*)}");

    //变量提取
    public static String pathVariables(String path,List<String> vs){

        //提取变量

        Matcher matcher = pattern.matcher(path);

        while (matcher.find()){
            String v = matcher.group(1);


            if (vs.contains(v)){
                throw new RuntimeException("重复路径参数:"+v);
            }

            v=v.trim();
            vs.add(v);
            path=path.replace(matcher.group(),"(?<"+v+">[^/]*)");
        }

        return path;
    }



}
